<?php

namespace app\shop\service\user;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

/**
 * 会员导出服务类
 */
class ExportService
{
    /**
     * 会员导出
     */
    public function userList($list,$otype=0)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //列宽
        $sheet->getColumnDimension('A')->setWidth(15);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('C')->setWidth(20);
        $sheet->getColumnDimension('G')->setWidth(20);
        $sheet->getColumnDimension('I')->setWidth(15);
        $sheet->getColumnDimension('J')->setWidth(20);
        $sheet->getColumnDimension('N')->setWidth(20);

        //设置工作表标题名称
        $sheet->setTitle('会员列表');
        $setdata = [];
        if($otype==0){
            $setdata = ['会员ID','昵称','手机号','来源','积分','余额','推荐人','累计积分','总邀请人数','推荐订单数量','国家','省份','城市','注册时间'];
        }else{
            $setdata = ['会员ID','昵称','手机号','推荐人','推荐人姓名','推荐人所属商户','国家','省份','城市','注册时间'];
        }
        $this->buildCellHeader($sheet,$setdata,1);

        // $sheet->setCellValue('A1', '昵称');
        // $sheet->setCellValue('B1', '会员ID');
        // if($otype==0){
        //     $sheet->setCellValue('C1', '来源');
        //     $sheet->setCellValue('D1', '积分');
        //     $sheet->setCellValue('E1', '余额');
        // }
        // $sheet->setCellValue('F1', '手机号');
        // $sheet->setCellValue('G1', '推荐人');
        // if($otype==0){
        //     $sheet->setCellValue('H1', '累计积分');
        //     $sheet->setCellValue('I1', '总邀请人数');
        //     $sheet->setCellValue('J1', '推荐订单数量');
        // }
        // $sheet->setCellValue('K1', '国家');
        // $sheet->setCellValue('L1', '省份');
        // $sheet->setCellValue('M1', '城市');
        // $sheet->setCellValue('N1', '注册时间');

        //填充数据
        $index = 0;
        $startline = 2;
        foreach ($list as $user) {
            $insideindex = $index + $startline;
            $valData = [];
            array_push($valData, $user['user_id']);
            array_push($valData, "\t" . $user['nickName'] . "\t");
            if($otype==0){
                array_push($valData, $user['reg_source']);
                array_push($valData, $user['points']);
                array_push($valData, $user['balance']);
            }
            array_push($valData, $user['mobile']);
            array_push($valData, !empty($user['referee']) ? ($user['referee']['user_id'].'-'.$user['referee']['nickName'].'-'.$user['referee']['mobile']) : '');
            array_push($valData, $user['referee_real_name']);
            array_push($valData, $user['shop_supplier_name_text']);
            if($otype==0){
                array_push($valData, $user['total_points']);
                array_push($valData, $user['total_invite']);
                array_push($valData, $user['total_invite_num']);
            }
            array_push($valData, $user['country']);
            array_push($valData, $user['province']);
            array_push($valData, $user['city']);
            array_push($valData, $user['create_time']);
            $this->buildCellHeader($sheet,$valData,$insideindex);
            // $sheet->setCellValue('A' . ($index + $startline), "\t" . $user['nickName'] . "\t");
            // $sheet->setCellValue('B' . ($index + $startline), $user['user_id']);
            // if($otype==0){
            //     $sheet->setCellValue('C' . ($index + $startline), $user['reg_source']);
            //     $sheet->setCellValue('D' . ($index + $startline), $user['points']);
            //     $sheet->setCellValue('E' . ($index + $startline), $user['balance']);
            // }
            // $sheet->setCellValue('F' . ($index + $startline), $user['mobile']);
            // $sheet->setCellValue('G' . ($index + $startline), !empty($user['referee']) ? ($user['referee']['user_id'].'-'.$user['referee']['nickName'].'-'.$user['referee']['mobile']) : '');
            // if($otype==0){
            //     $sheet->setCellValue('H' . ($index + $startline), $user['total_points']);
            //     $sheet->setCellValue('I' . ($index + $startline), $user['total_invite']);
            //     $sheet->setCellValue('J' . ($index + $startline), $user['total_invite_num']);
            // }
            // $sheet->setCellValue('K' . ($index + $startline), $user['country']);
            // $sheet->setCellValue('L' . ($index + $startline), $user['province']);
            // $sheet->setCellValue('M' . ($index + $startline), $user['city']);
            // $sheet->setCellValue('N' . ($index + $startline), $user['create_time']);
            $index++;
        }

        //保存文件
        $filename = iconv("UTF-8", "GB2312//IGNORE", '用户') . '-' . date('YmdHis') . '.xlsx';

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }

    /**
     * 会员导出
     */
    public function userListData($list)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //列宽
        $sheet->getColumnDimension('A')->setWidth(15);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('F')->setWidth(20);
        $sheet->getColumnDimension('G')->setWidth(20);
        $sheet->getColumnDimension('I')->setWidth(15);
        $sheet->getColumnDimension('J')->setWidth(20);
        $sheet->getColumnDimension('N')->setWidth(20);

        //设置工作表标题名称
        $sheet->setTitle('会员列表');
        $sheet->setCellValue('A1', '用户ID');
        $sheet->setCellValue('B1', '昵称');
        $sheet->setCellValue('C1', '手机号');
        $sheet->setCellValue('D1', '注册IP');
        $sheet->setCellValue('E1', '注册时间');

        //填充数据
        $index = 0;
        $startline = 2;
        foreach ($list as $user) {
            $sheet->setCellValue('A' . ($index + $startline), $user['user_id']);
            $sheet->setCellValue('B' . ($index + $startline), "\t" . $user['nickName'] . "\t");
            $sheet->setCellValue('C' . ($index + $startline), $user['mobile']);
            $sheet->setCellValue('D' . ($index + $startline), $user['reg_ip']);
            $sheet->setCellValue('E' . ($index + $startline), $user['create_time']);
            $index++;
        }
        //保存文件
        $filename = iconv("UTF-8", "GB2312//IGNORE", '用户') . '-' . date('YmdHis') . '.xlsx';

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }

    /**
     * 订单导出
     */
    public function userOrderList($list)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        //列宽
        $sheet->getColumnDimension('A')->setWidth(15);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('F')->setWidth(20);
        $sheet->getColumnDimension('G')->setWidth(20);
        $sheet->getColumnDimension('I')->setWidth(15);
        $sheet->getColumnDimension('J')->setWidth(20);
        $sheet->getColumnDimension('N')->setWidth(20);

        //设置工作表标题名称
        $sheet->setTitle('会员列表');

        $sheet->setCellValue('A1', '用户ID');
        $sheet->setCellValue('B1', '昵称');
        $sheet->setCellValue('C1', '手机号码');
        $sheet->setCellValue('D1', '总邀请人数');
        $sheet->setCellValue('E1', '总订单');
        $sheet->setCellValue('F1', '订单总额');
        $sheet->setCellValue('G1', '首单总单数');
        $sheet->setCellValue('H1', '首单总交易金额');
        $sheet->setCellValue('I1', '非首单总单数');
        $sheet->setCellValue('J1', '非首单总交易金额');
        $sheet->setCellValue('k1', '配送订单数量');
        $sheet->setCellValue('L1', '配送总金额');


        //填充数据
        $index = 0;
        $startline = 2;
        foreach ($list as $user) {
            $sheet->setCellValue('A' . ($index + $startline), $user['user_id']);
            $sheet->setCellValue('B' . ($index + $startline), "\t" . $user['nickName'] . "\t");
            $sheet->setCellValue('C' . ($index + $startline), "\t" . $user['mobile'] . "\t");
            $sheet->setCellValue('D' . ($index + $startline), $user['total_invite']);
            $sheet->setCellValue('E' . ($index + $startline), $user['total_order_nums']);
            $sheet->setCellValue('F' . ($index + $startline), $user['total_order_price']);
            $sheet->setCellValue('G' . ($index + $startline), $user['first_num']);
            $sheet->setCellValue('H' . ($index + $startline), $user['first_price']);
            $sheet->setCellValue('I' . ($index + $startline), $user['order_nums']);
            $sheet->setCellValue('J' . ($index + $startline), $user['order_price']);
            $sheet->setCellValue('K' . ($index + $startline), $user['delivery_order_num']);
            $sheet->setCellValue('L' . ($index + $startline), $user['delivery_price']);

            $index++;
        }

        //保存文件
        $filename = iconv("UTF-8", "GB2312//IGNORE", '推广员订单业绩统计') . '-' . date('YmdHis') . '.xlsx';

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }

    /**
     * @Exp    导出数据拼接
     * @Author Mr.Z
     * @param  [type]     &$sheet [description]
     * @param  [type]     $data   [description]
     * @param  [type]     $index  [description]
     * @return [type]             [description]
     */
    private function buildCellHeader(&$sheet, $data, $index, $startkey=0){
        foreach ($data as $key => $value) {
            $Akey = ABCString($startkey+$key);
            $sheet->setCellValue($Akey . $index, $value);
        }
    }
}
